const app = getApp();
Page({

  data: {
    GoodsList: [],
    noPaidGoodsList: [],
    noDeliveryGoodsList: [],
    noReceivedGoodsList: [],
    noEvaluateGoodsList: [],
    accomplishGoodsList: [],
    status: -1,
    numberReduceList:[]

  },
  onLoad: function (options) {
    this.getChangeGoodsList();
  },
  // 根据状态修改显示内容
  getChangeGoodsList() {
    wx.request({
      url: `${app.globalData.baseurl}order/incomplete`,
      method: 'POST',
      data: {
        openid: app.globalData.openid,
        status: this.data.status
      },
      success: (res) => {
        if (res.statusCode == 200) {
          let list = [];
          for (var i = 0, j = 0; i < res.data.length; i++) {
            if (res.data[i].openid == app.globalData.openid) {
              let orderListDetail = [];
              for (let g = 0; g < JSON.parse(res.data[i].orderList).length; g++) {
                orderListDetail.push(JSON.parse(res.data[i].orderList)[g]);
              }
              // console.log(orderListDetail);
              list.push({ orderListDetail });
              list[j].orderNewid = res.data[i].orderNewid;
              list[j].createtime = res.data[i].createtime;
              list[j].totalMoney = res.data[i].totalMoney;
              list[j].address = res.data[i].address;
              list[j].index = i + 1;
              j++;
            }
          }
          this.setData({
            GoodsList: list
          });
          console.log(list);
          if(this.data.status==1){
            const numberReduce=[];
            const numberList=[];
            const goodsNameList=[];
            for(let m=0;m<list.length;m++){
              for(let n=0;n<list[m].orderListDetail.length;n++){
                numberReduce.push(list[m].orderListDetail[n].goodsid);
                numberList.push(list[m].orderListDetail[n].number);
                goodsNameList.push(list[m].orderListDetail[n].goodsname);
              }
            }
            this.setData({
              numberReduceList:numberReduce,
              numberList:numberList,
              goodsNameList:goodsNameList,
            })
            this.reduceGoodsNumber(numberReduce,numberList);
            console.log(numberReduce);
          }
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  // 减少已发货的商品的数量
  reduceGoodsNumber(numberReduce,numberList){
    const numberReduceList=numberReduce;
    const numberLists=numberList;
    const nameList=this.data.goodsNameList;
    for(let i=0;i<numberReduceList.length;i++){
      wx.request({
        url: `${app.globalData.baseurl}order/reduceGoodsNumber`,
        method: "POST",
        data: {
          goodsid: numberReduceList[i],
          number:numberLists[i]
        },
        success: function (res) {
          console.log('更新商品数量成功！');
          if(res.data.changedRows==0){
            wx.showToast({
              // icon: 'error',
              icon:'none',
              title: nameList[i]+'商品暂时无货',
            })
          }
          console.log(res.data.changedRows==0);
        },
        fail:function(res){
          console.log(res);
        }
      })
    }
  },
  chooseType(event) {
    // console.log(event.currentTarget.dataset.type);
    let status = event.currentTarget.dataset.type;
    this.setData({
      status: status
    })
    this.getChangeGoodsList();
    // console.log(this.data.status);
  },
  // 删除订单
  deleteProject(event) {
    console.log(event.currentTarget.dataset);
    let id = event.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否删除该订单',
      confirmText: '确认'
    }).then(res => {
      console.log("删除成功");
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/deleteOrder`,
          method: "POST",
          data: {
            orderNewid: id,
            //-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            console.log(res.data);
            wx.navigateBack({
              delta: 0,
              success() {
                wx.showToast({
                  title: '删除成功',
                })
              }
            })

          }
        })
      } else {
        wx.showToast({
          icon: 'error',
          title: '暂不取消',
        })
      }

    })
  },
  // 支付按钮
  payProject(event) {
    // console.log(event.currentTarget.dataset);
    let payId = event.currentTarget.dataset.id;
    let sumPrice = event.currentTarget.dataset.sumprice;
    wx.showModal({
      title: '提示',
      content: '是否支付商品合计' + sumPrice + '元',
      confirmText: '支付'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: payId,
            status: 0,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            console.log(res.data);
            wx.navigateTo({
              url: '../myOrder/myOrder',
              success() {
                wx.showToast({
                  title: '支付成功',
                })
              }
            })

          }
        })
      } else {
        wx.showToast({
          icon: 'error',
          title: '支付取消',
        })
      }

    })
  },
  // 取消订单
  cancelOrder(event) {
    console.log(event.currentTarget.dataset.id);
    let id = event.currentTarget.dataset.id;
    wx.showModal({
      title: '取消订单',
      confirmText: '确认'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: id,
            status: -1,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            console.log(res.data);
            wx.navigateBack({
              delta: 0,
              success() {
                wx.showToast({
                  title: '取消成功',
                })
              }
            })
          }
        })
      } else {
        wx.showToast({
          icon: 'error',
          title: '取消失败',
        })
      }
    })
  },
  // 确认收货
  receivedGoods(event) {
    console.log(event.currentTarget.dataset.id);
    let id = event.currentTarget.dataset.id;
    wx.showModal({
      title: '确认收货',
      confirmText: '确认'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: id,
            status: 2,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            console.log(res.data);
            wx.navigateBack({
              // url: '../myOrder/myOrder',
              delta: 0,
              success() {
                wx.showToast({
                  title: '收货成功',
                })
              }
            })

          }
        })
      } else {
        wx.showToast({
          icon: 'error',
          title: '取消收货',
        })
      }
    })
  },
  // 评价
  evaluateGoods(event) {
    console.log(event.currentTarget.dataset);
    let id = event.currentTarget.dataset.id;
    let goodsId = event.currentTarget.dataset.goodsid;
    let goodsName = event.currentTarget.dataset.goodsname;
    let img = event.currentTarget.dataset.img;
    console.log(goodsName);
    console.log(img);
    // 去评价
    wx.showModal({
      title: '确认评价',
      confirmText: '确认'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: id,
            status: 2,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            console.log(res.data);
            wx.navigateTo({
              url: '../myOrder/comment/comment?id=' + goodsId + '&goodsName=' + goodsName + '&goodsimg=' + img,
            })
          }
        })
      } else {
        wx.showToast({
          icon: 'error',
          title: '取消评价',
        })
      }
    })
  },
  // 评价订单
  evaluateOrder(event) {
    console.log(event.currentTarget.dataset);
    let id = event.currentTarget.dataset.id;
    // 去评价
    wx.showModal({
      title: '确认订单完成',
      confirmText: '确认'
    }).then(res => {
      console.log(res);
      if (res.confirm == true) {
        wx.request({
          url: `${app.globalData.baseurl}order/changeStatus`,
          method: "POST",
          data: {
            orderNewid: id,
            status: 3,//-1：取消订单 0:待发货，1;待收货+已发货，2：待评价，3：已完成
          },
          success: function (res) {
            wx.navigateBack({
              delta: 0,
            })
          }
        })

      } else {
        wx.showToast({
          icon: 'error',
          title: '取消',
        })
      }
    })
  }
})